package mes.kanban.controller;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import mes.kanban.entity.Customer;
import mes.kanban.entity.MesRoleAndJob;
import mes.kanban.entity.MesRoleAndPermission;
import mes.kanban.entity.MesUserAndRole;
import mes.kanban.service.CustomerServiceI;
import mes.kanban.util.PageBean;
import mes.kanban.vo.AjaxReturnVO;
import mes.kanban.vo.MesRoleAndPermissionVO;
import mes.kanban.vo.MesUserAndRoleVO;


/**
 * 客户管理
 * @author kevin.lam
 *
 */
@Controller
@RequestMapping(value="/customer")
public class CustomerController implements Serializable{

	private static final long serialVersionUID = 2131046413561732053L;
	
	private static final Logger log = LoggerFactory.getLogger(CustomerController.class);
	
	
	@Autowired
	private CustomerServiceI customerServiceI;
	
	/**
	 * 跳转到客户管理页面
	 * @return
	 */
	@GetMapping(value="/toList")
	public String gotoRoleManage() {
		log.info("客户管理界面");
		return "/user/role";
	}
	
	@GetMapping(value="/toCustomerList")
	public String gotoCustomerManage() {
		log.info("客户管理界面");
		return "/user/customerList";
	}
	
	/**
	 * 分页查询客户列表
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping(value="/list")
	@ResponseBody
	public AjaxReturnVO getRole(@RequestParam("pageNum")Integer pageNum,@RequestParam("pageSize")Integer pageSize) {
		
		log.info("查询客户列表信息");
		
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			vo.setOk(true);
			PageBean pb = this.customerServiceI.getRolesForBean(pageNum,pageSize);
			vo.setData(pb);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setOk(false);
			vo.setMsg("报错了");
		}
		
		return vo;
	}
	
	/**
	 * 添加客户信息
	 * @param vo
	 * @return
	 */
    @PostMapping("/add")
    @ResponseBody
    public AjaxReturnVO add(@RequestBody Customer vo) {
    	log.info("添加客户配置信息");
		AjaxReturnVO ajaxVo = new AjaxReturnVO();
		try {
			this.customerServiceI.save(vo);
	    	ajaxVo.setData(vo);
	    	ajaxVo.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxVo.setMsg("请求失败");
			ajaxVo.setOk(false);
		}
    	return ajaxVo;
    }

    /**
     * 编辑客户信息
     * @param vo
     * @return
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxReturnVO edit(@RequestBody Customer vo) {
    	log.info("修改客户配置信息");
		AjaxReturnVO ajaxVo = new AjaxReturnVO();
		try {
			this.customerServiceI.edit(vo);
	    	ajaxVo.setData(vo);
	    	ajaxVo.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			ajaxVo.setMsg("请求失败");
			ajaxVo.setOk(false);
		}
    	return ajaxVo;
    }
    
    /**
     * 删除客户
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ResponseBody
    public AjaxReturnVO deleteById(@RequestParam("id") String id) {
    	AjaxReturnVO j = new AjaxReturnVO();
    	try {
	    	this.customerServiceI.deleteRoleById(id);
	    	j.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
		}
    	return j;
    }
	
	/**
     * 查询用户对应的客户
     * @param id
     * @return
     */
    @GetMapping("/getRoleListByUserId")
    @ResponseBody
    public AjaxReturnVO getRoleListByUserId(@RequestParam("id") String id) {
    	
    	log.info("根据用户ID查询客户列表信息");
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			vo.setOk(true);
			Map<String,Object> map = new HashMap<String,Object>();
			List<MesUserAndRoleVO> left = this.customerServiceI.getRoleList();
			List<MesUserAndRoleVO> right = this.customerServiceI.getRoleListByUserId(id);
			map.put("left", left);
			map.put("right", right);
			map.put("userId", id);
			vo.setAttrs(map);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setOk(false);
			vo.setMsg("查询失败");
		}
		
		return vo;
    	
    }
    
    /**
     * 保存客户用户关系
     * @param roleList
     * @return
     */
    @PostMapping(value="/saveUserAndRole")
    @ResponseBody
    public AjaxReturnVO saveUserAndRole(@RequestBody List<MesUserAndRole> roleList) {
    	
    	AjaxReturnVO vo = new AjaxReturnVO();
    	
    	try {
			log.info("保存用户客户");
    		vo.setOk(true);
    		this.customerServiceI.saveUserAndRole(roleList);
		} catch (Exception e) {
			vo.setOk(false);
			e.printStackTrace();
			vo.setMsg("查询失败");
		}
    	return vo;
    }
    
    @GetMapping(value="/deleteRoleByuserId")
    @ResponseBody
    public AjaxReturnVO deleteRoleByuserId(@RequestParam("userId")String userId) {
    	AjaxReturnVO vo = new AjaxReturnVO();
    	
    	try {
			log.info("删除用户与客户");
    		vo.setOk(true);
    		this.customerServiceI.deleteRoleByuserId(userId);
		} catch (Exception e) {
			vo.setOk(false);
			e.printStackTrace();
			vo.setMsg("删除失败");
		}
    	return vo;
    	
    }
    
    /**
     * 查询客户对应的菜单权限
     * @param id
     * @return
     */
    @GetMapping("/getPermissionByRoleId")
    @ResponseBody
    public AjaxReturnVO getPermissionByRoleId(@RequestParam("id") String id) {
    	
    	log.info("根据客户ID查询菜单列表信息");
		AjaxReturnVO vo = new AjaxReturnVO();
		
		try {
			vo.setOk(true);
			Map<String,Object> map = new HashMap<String,Object>();
			List<MesRoleAndPermissionVO> left = this.customerServiceI.getPermissionList();
			List<MesRoleAndPermissionVO> right = this.customerServiceI.getPermissionByRoleId(id);
			map.put("left", left);
			map.put("right", right);
			map.put("roleId", id);
			vo.setAttrs(map);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setOk(false);
			vo.setMsg("查询失败");
		}
		
		return vo;
    	
    }
    
    /**
     * 保存客户菜单关系
     * @param permissionList
     * @return
     */
    @PostMapping(value="/saveRoleAndPeimission")
    @ResponseBody
    public AjaxReturnVO saveRoleAndPermission(@RequestBody List<MesRoleAndPermission> permissionList) {
    	
    	AjaxReturnVO vo = new AjaxReturnVO();
    	
    	try {
    		vo.setOk(true);
    		this.customerServiceI.saveRoleAndPermission(permissionList);
		} catch (Exception e) {
			vo.setOk(false);
			e.printStackTrace();
			vo.setMsg("查询失败");
		}
    	return vo;
    }
    
    /**
     * 根据客户Id删除作业权限
     */
    @GetMapping(value="/deleteRoleAndPermissionByRoleId")
    @ResponseBody
    public AjaxReturnVO deleteRoleAndPermissionByRoleId(@RequestParam("id")  String id) {
    	
    	AjaxReturnVO vo = new AjaxReturnVO();
    	
    	try {
    		vo.setOk(true);
    		this.customerServiceI.deleteRoleAndPermissionByRoleId(id);
		} catch (Exception e) {
			vo.setOk(false);
			e.printStackTrace();
			vo.setMsg("删除失败");
		}
    	return vo;
    }
    
    /**
     * 保存客户菜单关系
     * @param permissionList
     * @return
     */
    @PostMapping(value="/saveRoleAndJob")
    @ResponseBody
    public AjaxReturnVO saveRoleAndJob(@RequestBody List<MesRoleAndJob> jobList) {
    	
    	AjaxReturnVO vo = new AjaxReturnVO();
    	
    	try {
    		vo.setOk(true);
    		this.customerServiceI.saveRoleAndJob(jobList);
		} catch (Exception e) {
			vo.setOk(false);
			e.printStackTrace();
			vo.setMsg("查询失败");
		}
    	return vo;
    }
    
    /**
     * 根据客户Id删除作业权限
     */
    @GetMapping(value="/deleteRoleAndJobByRoleId")
    @ResponseBody
    public AjaxReturnVO deleteRoleAndJobByRoleId(@RequestParam("id")  String id) {
    	
    	AjaxReturnVO vo = new AjaxReturnVO();
    	
    	try {
    		vo.setOk(true);
    		this.customerServiceI.deleteRoleAndJobByRoleId(id);
		} catch (Exception e) {
			vo.setOk(false);
			e.printStackTrace();
			vo.setMsg("删除失败");
		}
    	return vo;
    }
    
	
}
